package com.iemes.controller.quality;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.QualityAbnormalFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.quality.QualityAbnormalMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/quality")
@Controller
public class QualityAbnormalController extends BaseController {

	@Inject
	private QualityAbnormalMapper qualityAbnormalMapper;
	
	@RequestMapping("qualityabnormal")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		
		//显示客户、业务员和责任部门
		List<ClientFormMap> clientFormMaps=baseMapper.findByWhere(new ClientFormMap());
		model.addAttribute("clientFormMaps", clientFormMaps);
		List<DepartmentForMap> departmentForMaps=baseMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("departmentForMaps", departmentForMaps);
		List<UserFormMap> userFormMaps=qualityAbnormalMapper.getUserBusiness();
		model.addAttribute("userFormMaps", userFormMaps);
		return Common.BACKGROUND_PATH + "/quality/qualityabnormal/qualityabnormal_list";
	}
	
	
	@ResponseBody
	@RequestMapping("qualityabnormal_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		QualityAbnormalFormMap qualityAbnormalFormMap=getFormMap(QualityAbnormalFormMap.class);
		List<Map<String,Object>> qualityAbnormalFormMaps=qualityAbnormalMapper.getQualityAbnormalByCondition(qualityAbnormalFormMap);
		LuogicPaging(pageNow, pageSize, qualityAbnormalFormMaps);
		return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping(value="qualityabnormal_deleteEntity")
	@SystemLog(module="日常品质异常",methods="品质异常-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		QualityAbnormalFormMap qualityAbnormalFormMap=getFormMap(QualityAbnormalFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			qualityAbnormalFormMap.put("state", 1);
			qualityAbnormalFormMap.put("id", str[i]);
			baseMapper.editEntity(qualityAbnormalFormMap);
		}
		return "success";
	}
	
	@RequestMapping("qualityabnormal_addUI")
	public String addUI(Model model) throws Exception {
		//显示客户
		List<ClientFormMap> clientFormMaps=baseMapper.findByWhere(new ClientFormMap());
		model.addAttribute("clientFormMaps", clientFormMaps);
		//部门
		List<DepartmentForMap> departmentForMaps=baseMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("departmentForMaps", departmentForMaps);
		//业务员
		List<UserFormMap> business=qualityAbnormalMapper.getUserBusiness();
		model.addAttribute("business", business);
		//qa
		List<UserFormMap> qa=qualityAbnormalMapper.getUserQA();
		model.addAttribute("qa", qa);
		//qc
		List<UserFormMap> qc=qualityAbnormalMapper.getUserQC();
		model.addAttribute("qc", qc);
		//包装班长
		List<UserFormMap> monitor=qualityAbnormalMapper.getUserMonitor();
		model.addAttribute("monitor", monitor);
		return Common.BACKGROUND_PATH + "/quality/qualityabnormal/qualityabnormal_add";
	}
	
	
	@ResponseBody
	@RequestMapping("qualityabnormal_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="日常品质异常",methods="品质异常-新增")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		QualityAbnormalFormMap qualityAbnormalFormMap=getFormMap(QualityAbnormalFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		qualityAbnormalFormMap.put("date", sdf.format(date));
		qualityAbnormalFormMap.put("state", 0);
		baseMapper.addEntity(qualityAbnormalFormMap);
		return "success";
	}
	
	
	@RequestMapping("qualityabnormal_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		QualityAbnormalFormMap qualityAbnormalFormMap=qualityAbnormalMapper.getQualityAbnormalById(Integer.parseInt(id));
		model.addAttribute("qualityAbnormalFormMap", qualityAbnormalFormMap);
		
		//显示客户
		List<ClientFormMap> clientFormMaps = baseMapper.findByWhere(new ClientFormMap());
		model.addAttribute("clientFormMaps", clientFormMaps);
		// 部门
		List<DepartmentForMap> departmentForMaps = baseMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("departmentForMaps", departmentForMaps);
		// 业务员
		List<UserFormMap> business = qualityAbnormalMapper.getUserBusiness();
		model.addAttribute("business", business);
		// qa
		List<UserFormMap> qa = qualityAbnormalMapper.getUserQA();
		model.addAttribute("qa", qa);
		// qc
		List<UserFormMap> qc = qualityAbnormalMapper.getUserQC();
		model.addAttribute("qc", qc);
		// 包装班长
		List<UserFormMap> monitor = qualityAbnormalMapper.getUserMonitor();
		model.addAttribute("monitor", monitor);
		
		return Common.BACKGROUND_PATH + "/quality/qualityabnormal/qualityabnormal_edit";
	}
	
	
	@ResponseBody
	@RequestMapping(value="qualityabnormal_editEntity")
	@SystemLog(module="日常品质异常",methods="品质异常-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		QualityAbnormalFormMap qualityAbnormalFormMap=getFormMap(QualityAbnormalFormMap.class);
		baseMapper.editEntity(qualityAbnormalFormMap);
		return "success";
	}
	
}
